package migrate

import (
	"advertising/config/mysql"
	"advertising/internal/define"
	"advertising/internal/models"

	"gorm.io/gorm"
)

type MigrateFunc struct{}

func (MigrateFunc) Script1() error {
	return nil
}

func (MigrateFunc) Script2() error {
	mysql.NewDB().AutoMigrate(&models.PublishDevice{})
	mysql.NewDB().AutoMigrate(&models.PublishDeviceGroup{})
	mysql.NewDB().AutoMigrate(&models.MerchantPublishDeviceGroup{})
	mysql.NewDB().AutoMigrate(&models.PublishSchedule{})
	mysql.NewDB().AutoMigrate(&models.PublishScheduleDevice{})
	mysql.NewDB().AutoMigrate(&models.PublishScheduleMaterial{})
	mysql.NewDB().AutoMigrate(&models.PublishScheduleAuditLog{})
	return nil
}

func (MigrateFunc) Script3() error {
	mysql.NewDB().Migrator().AddColumn(&models.PublishScheduleMaterial{}, "repeat_times")
	mysql.NewDB().Migrator().AddColumn(&models.Material{}, "file_size")
	mysql.NewDB().Migrator().AddColumn(&models.PublishSchedule{}, "gender")
	mysql.NewDB().Migrator().AddColumn(&models.PublishSchedule{}, "age")
	mysql.NewDB().Migrator().AddColumn(&models.PublishSchedule{}, "tag")
	mysql.NewDB().Migrator().AddColumn(&models.PublishSchedule{}, "publish_type")
	mysql.NewDB().Migrator().AddColumn(&models.PublishSchedule{}, "publish_count")
	return nil
}

func (MigrateFunc) Script4() error {
	mysql.NewDB().Migrator().AddColumn(&models.PublishScheduleDevice{}, "deleted_at")
	mysql.NewDB().Migrator().AddColumn(&models.PublishScheduleMaterial{}, "deleted_at")
	mysql.NewDB().AutoMigrate(&models.PublishScheduleLog{})
	mysql.NewDB().Migrator().AddColumn(&models.PublishSchedule{}, "publish_used_count")
	mysql.NewDB().Migrator().AddColumn(&models.PublishSchedule{}, "device_count")
	mysql.NewDB().Migrator().AddColumn(&models.PublishSchedule{}, "details_show")
	return nil
}

func (MigrateFunc) Script5() error {
	db := mysql.NewDB()
	db.AutoMigrate(&models.Material{})
	if !db.Migrator().HasTable("material_group") {
		db.AutoMigrate(&models.MaterialFolder{})
	} else {
		db.Migrator().RenameTable("material_group", "material_folder")
	}
	db.Migrator().AddColumn(&models.Material{}, "group_id")
	db.Migrator().RenameColumn(&models.Material{}, "group_id", "folder_id")
	return nil
}

func (MigrateFunc) Script6() error {
	db := mysql.NewDB()
	if !db.Migrator().HasIndex(&models.PublishScheduleDevice{}, "idx_robotName_deletedAt") {
		db.Migrator().CreateIndex(&models.PublishScheduleDevice{}, "idx_robotName_deletedAt")
	}
	if !db.Migrator().HasIndex(&models.PublishSchedule{}, "idx_startTime") {
		db.Migrator().CreateIndex(&models.PublishSchedule{}, "idx_startTime")
	}
	if !db.Migrator().HasIndex(&models.PublishSchedule{}, "idx_endTime") {
		db.Migrator().CreateIndex(&models.PublishSchedule{}, "idx_endTime")
	}

	return nil
}

func (MigrateFunc) Script7() error {
	db := mysql.NewDB()
	if !db.Migrator().HasColumn(&models.PublishScheduleMaterial{}, "sort") {
		db.Migrator().AddColumn(&models.PublishScheduleMaterial{}, "sort")
	}
	if !db.Migrator().HasColumn(&models.PublishScheduleMaterial{}, "play_type") {
		db.Migrator().AddColumn(&models.PublishScheduleMaterial{}, "play_type")
	}
	if !db.Migrator().HasColumn(&models.PublishScheduleMaterial{}, "regular_interval") {
		db.Migrator().AddColumn(&models.PublishScheduleMaterial{}, "regular_interval")
	}
	return nil
}

func (MigrateFunc) Script202403() error {
	db := mysql.NewDB()
	tx := db.Set("gorm:table_options", "ENGINE=InnoDB").
		Set("gorm:table_options", "CHARSET=utf8mb4").
		Set("gorm:table_options", "COLLATE=utf8mb4_general_ci").
		Session(&gorm.Session{})
	tx.AutoMigrate(&models.PublishScheduleDevicePlaylist{})
	return nil
}

func (MigrateFunc) Script202408() error {
	db := mysql.NewDB()
	tx := db.Set("gorm:table_options", "ENGINE=InnoDB").
		Set("gorm:table_options", "CHARSET=utf8mb4").
		Set("gorm:table_options", "COLLATE=utf8mb4_general_ci").
		Session(&gorm.Session{})
	tx.AutoMigrate(&models.PromotionCount{})
	return nil
}

func (MigrateFunc) Script202510() error {
	db := mysql.NewDB()
	tx := db.Set("gorm:table_options", "ENGINE=InnoDB").
		Set("gorm:table_options", "CHARSET=utf8mb4").
		Set("gorm:table_options", "COLLATE=utf8mb4_general_ci").
		Session(&gorm.Session{})

	tx.Migrator().AddColumn(&models.PublishSchedule{}, "order_id")
	tx.Migrator().AddColumn(&models.PublishSchedule{}, "pay_amount")
	tx.Migrator().AddColumn(&models.PublishSchedule{}, "paid_amount")
	tx.Migrator().AddColumn(&models.PublishSchedule{}, "discount_amount")
	tx.Migrator().AddColumn(&models.PublishSchedule{}, "custom_name")
	tx.Migrator().AddColumn(&models.PublishSchedule{}, "phone_number")
	tx.Migrator().AddColumn(&models.PublishSchedule{}, "support_activity_id")
	err := tx.Migrator().AutoMigrate(
		&models.PublishSchedule{})
	if err == nil {

	}

	tx.Migrator().AddColumn(&models.PublishDeviceGroup{}, "type")
	err = tx.Migrator().AutoMigrate(
		&models.PublishDeviceGroup{})
	if err == nil {

	}

	// 示例：插入新记录（仅当 name + type 不存在时）
	newGroup := models.PublishDeviceGroup{
		Name: "小程序设备组",
		Type: define.DEVICEGROUP_MINIAPP,
	}

	// 使用 FirstOrCreate 避免重复插入
	result := tx.
		Where("name = ? AND type = ?", newGroup.Name, newGroup.Type).
		FirstOrCreate(&newGroup)
	if result.Error != nil {
		return result.Error
	}

	err = tx.Migrator().AutoMigrate(
		&models.SupportActivity{})
	if err == nil {

	}
	// helper.Must(err)
	return nil
}
